package com.chilly.jdkNio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 因此读取文件涉及三个步骤：
 * (1) 从 FileInputStream 获取 Channel，
 * (2) 创建 Buffer，
 * (3) 将数据从 Channel 读到 Buffer 中。
 * <p>
 * Created by Chilly Cui on 2020/12/28.
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("test.txt");
        FileChannel fileChannel = in.getChannel(); // FileInputStream 获取通道

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//创建缓冲区

        fileChannel.read(byteBuffer);//数据从通道读到缓冲区中

        in.close();
        fileChannel.close();
    }
}
